﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Main
{
    public class Drawer
    {

        private int offsetX = 15;
        private int offSetY = 0;
        private Floor _Floor;
        private Entity _Player;


        public Drawer(Floor floor, Entity player)
        {
            _Floor = floor;
            _Player = player;
        }

        public void DrawMenu()
        {
            Console.CursorVisible = false;
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(_Player.Race);
            Console.CursorLeft = 0;
            Console.WriteLine(_Player.Class);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("LEVEL");
            Console.WriteLine("XP");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("GOLD");
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("AC");
            Console.WriteLine("MAX HP");
            Console.WriteLine("HP");
        }


        public void DrawStats()
        {
            int offsetX = 14;
            Console.ForegroundColor = ConsoleColor.Green;
            //Level
            Console.CursorLeft = offsetX - _Player.Level.ToString().Length;
            Console.CursorTop = 2;
            Console.Write(_Player.Level);
            //XP
            Console.CursorLeft = offsetX - _Player.XP.ToString().Length;
            Console.CursorTop = 3;
            Console.Write(_Player.XP);
            //GOLD
            Console.CursorLeft = offsetX - _Player.Gold.ToString().Length;
            Console.CursorTop = 4;
            Console.Write(_Player.Gold);
            //AC
            Console.CursorLeft = offsetX - _Player.AC.ToString().Length;
            Console.CursorTop = 6;
            Console.Write(_Player.AC);
            //MAX HP
            Console.CursorLeft = offsetX - _Player.MaxHP.ToString().Length;
            Console.CursorTop = 7;
            Console.Write(_Player.MaxHP);
            //HP
            Console.CursorLeft = offsetX - _Player.HP.ToString().Length;
            Console.CursorTop = 8;
            Console.Write(_Player.HP);


            Console.ForegroundColor = ConsoleColor.White;


        }



        public void Draw()
        {
            Console.CursorLeft = 0;
            Console.CursorTop = 0;

            for (int y = 0; y < Floor.FloorH; y++)
            {
                for (int x = 0; x < Floor.FloorW; x++)
                {
                    var map = _Floor.Map;
                    var cell = map[x, y];
                    if (cell.Changed)
                    {
                        Console.CursorLeft = x + offsetX;
                        Console.CursorTop = y + offSetY;
                        Console.ForegroundColor = cell.ForegroundColor;
                        Console.BackgroundColor = cell.BackgroundColor;
                        Console.Write(cell.Character);
                        cell.Changed = false;
                    }
                }
            }

            Console.CursorLeft = 0;
            Console.CursorTop = 0;
        }


        public void DrawPlayer()
        {
            int x = _Player.Pos.X;
            int y = _Player.Pos.Y;
            var map = _Floor.Map;
            var cell = map[x, y];
            cell.Entity = _Player;
            cell.Changed = true;
            Console.CursorLeft = x + offsetX;
            Console.CursorTop = y + offSetY;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write(_Player.Character);
            Console.ForegroundColor = ConsoleColor.White;

        }

        public void MovePlayer(EMov dir)
        {
            int x = _Player.Pos.X;
            int y = _Player.Pos.Y;
            var map = _Floor.Map;
            var cell = map[x, y];
            cell.Changed = true;
            switch (dir)
            {
                case EMov.Up:
                    if (y > 0)
                    {
                        if (!map[x, y - 1].Blocks)
                        {
                            _Player.Pos.Y -= 1;
                            y = _Player.Pos.Y;
                            cell.Entity = null;                                 
                        }
                    }
                    break;
                case EMov.Down:
                    if (y < Floor.FloorH)
                    {
                        if (!map[x, y + 1].Blocks)
                        {
                            _Player.Pos.Y += 1;
                            y = _Player.Pos.Y;
                            cell.Entity = null;                            
                        }
                    }
                    break;
                case EMov.Left:
                    if (x > 0)
                    {
                        if (!map[x - 1, y].Blocks)
                        {
                            _Player.Pos.X -= 1;
                            x = _Player.Pos.X;
                            cell.Entity = null;
                        }
                    }
                    break;
                case EMov.Right:
                    if (x < Floor.FloorW)
                    {
                        if (!map[x + 1, y].Blocks)
                        {
                            _Player.Pos.X += 1;
                            x = _Player.Pos.X;
                            cell.Entity = null;
                        }
                    }
                    break;
                default:
                    break;
            }
        }


    }
}
